package gui;
import javax.swing.JLabel;
import javax.swing.GroupLayout.Alignment;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;

import javax.swing.JComboBox;

import classes.ConnectionDB;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * QuestionAdd.java
 *
 * Created on Aug 24, 2011, 5:47:17 PM
 */
/**
 *
 * @author ANTONIO
 */
public class QuestionAdd extends javax.swing.JFrame {

	private JFrame myFrame;
	private String lastChosenTopic;
    /** Creates new form QuestionAdd 
     * @param langFile 
     * @throws SQLException */
    public QuestionAdd(Properties langFile) throws SQLException {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     * @throws SQLException 
     */
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() throws SQLException {
    	final Connection connection = new ConnectionDB().createConnection();
    	
    	this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    	this.setUndecorated(true);
    	myFrame = this;
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        
        JLabel lblQuestionName = new JLabel("Question name:");
        lblQuestionName.setFont(new Font("Tahoma", Font.BOLD, 15));
        
        qName = new JTextField();
        qName.setColumns(10);
        
        JLabel lblQuestionWeight = new JLabel("Question weight :");
        lblQuestionWeight.setFont(new Font("Tahoma", Font.BOLD, 15));
        
        qWeight = new JTextField();
        qWeight.setColumns(10);
        
        JButton btnQuestionBody = new JButton("Question body");
        btnQuestionBody.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		new FileEditor().main(null);
        	}
        });
        
        JButton btnAnswer = new JButton("Answer #1");
        btnAnswer.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		new FileEditor().main(null);
        	}
        });
        
        JButton btnNewButton = new JButton("Answer #3");
        btnNewButton.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		new FileEditor().main(null);
        	}
        });
                
        JButton btnAnswer_1 = new JButton("Answer #2");
        btnAnswer_1.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		new FileEditor().main(null);
        	}
        });
        
        JButton btnAnswer_2 = new JButton("Answer #4");
        btnAnswer_2.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		new FileEditor().main(null);
        	}
        });
        
        JLabel lblTopic = new JLabel("Topic :");
        lblTopic.setFont(new Font("Tahoma", Font.BOLD, 15));
        
        final JComboBox comboBox = new JComboBox();
        comboBox.addFocusListener(new FocusAdapter() {
        	@Override
        	public void focusGained(FocusEvent arg0) {
        		final Connection connection = new ConnectionDB().createConnection();

        		java.sql.Statement statemnt;
				try {
	        		ResultSet ress;   
					statemnt = connection.createStatement();
					ress = statemnt.executeQuery("SELECT DISTINCT topic FROM TOPSUB");
        			ArrayList<String> topics = new ArrayList<String>();

        			while (ress.next()) {
        				topics.add(ress.getString("topic"));
        			}
        			int i = 0;
        			String item = (String) comboBox.getItemAt(i);
        			while (item != null) {
        				if (topics.contains(item))
        					topics.remove(item);
        				i++;
        				item = (String) comboBox.getItemAt(i);
        			}
        			for (String str : topics) {
        				comboBox.addItem(str);
        			}
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}    	
        	}
        });
        
        JLabel lblSubtopic = new JLabel("Subtopic :");
        lblSubtopic.setFont(new Font("Tahoma", Font.BOLD, 15));
        
        final JComboBox comboBox_1 = new JComboBox();
        
        comboBox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				String chosen = (String) comboBox.getSelectedItem();
				lastChosenTopic = chosen;
				
				comboBox_1.removeAllItems();
				comboBox_1.addItem("--------");
				java.sql.PreparedStatement statMent;
				try {
					statMent = connection
							.prepareStatement("SELECT subtopic FROM TOPSUB WHERE topic = ? AND NOT(subtopic IS NULL)");
					statMent.setString(1, lastChosenTopic);
					ResultSet res = statMent.executeQuery();

					ArrayList<String> subTopics = new ArrayList<String>();

					while (res.next()) {
						subTopics.add(res.getString("subtopic"));
					}

					for (String s : subTopics) {
						comboBox_1.addItem(s);
					}
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				
			}
		});
        
        JLabel lblNumberOfPoints = new JLabel("Number of points :");
        lblNumberOfPoints.setFont(new Font("Tahoma", Font.BOLD, 15));
        
        textField = new JTextField();
        textField.setColumns(10);
        
        javax.swing.GroupLayout gl_jPanel1 = new javax.swing.GroupLayout(jPanel1);
        gl_jPanel1.setHorizontalGroup(
        	gl_jPanel1.createParallelGroup(Alignment.LEADING)
        		.addGroup(gl_jPanel1.createSequentialGroup()
        			.addGap(18)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.LEADING)
        				.addGroup(gl_jPanel1.createSequentialGroup()
        					.addComponent(btnAnswer)
        					.addGap(34)
        					.addComponent(btnAnswer_1)
        					.addGap(53)
        					.addComponent(btnNewButton)
        					.addGap(52)
        					.addComponent(btnAnswer_2))
        				.addGroup(gl_jPanel1.createSequentialGroup()
        					.addGroup(gl_jPanel1.createParallelGroup(Alignment.LEADING)
        						.addGroup(gl_jPanel1.createParallelGroup(Alignment.TRAILING, false)
        							.addGroup(gl_jPanel1.createSequentialGroup()
        								.addComponent(lblTopic)
        								.addPreferredGap(ComponentPlacement.RELATED)
        								.addComponent(comboBox, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        							.addComponent(lblQuestionName, Alignment.LEADING))
        						.addComponent(lblQuestionWeight)
        						.addComponent(btnQuestionBody, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE))
        					.addGap(35)
        					.addGroup(gl_jPanel1.createParallelGroup(Alignment.LEADING)
        						.addGroup(gl_jPanel1.createSequentialGroup()
        							.addComponent(lblNumberOfPoints)
        							.addPreferredGap(ComponentPlacement.RELATED)
        							.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        						.addComponent(qWeight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        						.addGroup(gl_jPanel1.createSequentialGroup()
        							.addComponent(lblSubtopic)
        							.addPreferredGap(ComponentPlacement.UNRELATED)
        							.addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE))
        						.addComponent(qName, GroupLayout.PREFERRED_SIZE, 265, GroupLayout.PREFERRED_SIZE))))
        			.addContainerGap(190, Short.MAX_VALUE))
        );
        gl_jPanel1.setVerticalGroup(
        	gl_jPanel1.createParallelGroup(Alignment.LEADING)
        		.addGroup(gl_jPanel1.createSequentialGroup()
        			.addGap(45)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblQuestionName)
        				.addComponent(qName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(43)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblTopic)
        				.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(lblSubtopic)
        				.addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(56)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblQuestionWeight)
        				.addComponent(qWeight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.TRAILING)
        				.addComponent(btnQuestionBody)
        				.addGroup(gl_jPanel1.createParallelGroup(Alignment.BASELINE)
        					.addComponent(lblNumberOfPoints)
        					.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
        			.addGap(73)
        			.addGroup(gl_jPanel1.createParallelGroup(Alignment.BASELINE)
        				.addComponent(btnAnswer)
        				.addComponent(btnAnswer_1)
        				.addComponent(btnNewButton)
        				.addComponent(btnAnswer_2))
        			.addContainerGap(72, Short.MAX_VALUE))
        );
        jPanel1.setLayout(gl_jPanel1);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        jLabel1.setText("Add question");
        
        JButton btnCancel = new JButton("Cancel");
        btnCancel.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		myFrame.setVisible(false);
        	}
        });
        
        JButton btnNewButton_1 = new JButton("Insert");
        btnNewButton_1.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        	}
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        layout.setHorizontalGroup(
        	layout.createParallelGroup(Alignment.TRAILING)
        		.addGroup(layout.createSequentialGroup()
        			.addGap(585)
        			.addComponent(jLabel1, GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
        			.addGap(119))
        		.addGroup(Alignment.LEADING, layout.createSequentialGroup()
        			.addGap(192)
        			.addGroup(layout.createParallelGroup(Alignment.LEADING)
        				.addGroup(layout.createSequentialGroup()
        					.addComponent(btnCancel)
        					.addPreferredGap(ComponentPlacement.RELATED, 535, Short.MAX_VALUE)
        					.addComponent(btnNewButton_1))
        				.addComponent(jPanel1, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        			.addGap(229))
        );
        layout.setVerticalGroup(
        	layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        			.addGap(73)
        			.addComponent(jLabel1)
        			.addGap(92)
        			.addComponent(jPanel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        			.addGap(47)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(btnCancel)
        				.addComponent(btnNewButton_1))
        			.addGap(173))
        );
        getContentPane().setLayout(layout);

        pack();
    }// </editor-fold>

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
					new QuestionAdd(null).setVisible(true);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private JTextField qName;
    private JTextField qWeight;
    private JTextField textField;
}
